package com.sto.stoapp.camera;




import java.io.ByteArrayOutputStream;
import java.io.IOException;


import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Size;
import android.view.SurfaceHolder;

public class CameraOperation implements AutoFocusCallback {
	static CameraOperation m_instance;

	private Camera m_camera = null;

	public static Camera f_camera;

	// false不让PreViewCallback获取图片数据 、true让PreViewCallback获取图片数据
	private boolean isOver = false;

	// 接口对象
	private OnPreviewListener onPreviewListener;

	// 获取第二次的图片
	private int count = 3;

	public static CameraOperation instance() {
		if (m_instance == null) {
			m_instance = new CameraOperation();
		}
		return m_instance;
	}

	/**
	 * 构造函数
	 */
	private CameraOperation() {

	}

	/**
	 * 打开相机线程
	 * 
	 * @author hb
	 * 
	 */
	private class OpenCameraThread extends Thread {

		@Override
		public void run() {
			f_camera = Camera.open();
			super.run();
		}
	}

	/**
	 * 打开相机
	 */
	public void openCameraThread() {
		new OpenCameraThread().start();
	}

	/**
	 * 关闭相机
	 */
	public void releaseCamera() {
		if (f_camera != null) {
			f_camera.release();
			f_camera = null;
		}
		m_instance = null;
	}

	/**
	 * 初始化摄像头设备
	 * 
	 * @param holder
	 */
	public void initialPreview(SurfaceHolder holder) {
		try {
			open();
			if (m_camera != null) {
				m_camera.setDisplayOrientation(90);
				m_camera.setPreviewDisplay(holder);
				m_camera.setPreviewCallback(new PreViewCallback());
				m_camera.startPreview();// 开始预览
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	public void autofocus() {
		if (m_camera != null) {
			m_camera.autoFocus(this);
		}
	}

	// public void startPreview(int width, int height) {
	// try {
	// if (m_camera != null) {
	// Camera.Parameters parameters = m_camera.getParameters();
	// m_camera.setDisplayOrientation(90);
	// parameters.setJpegQuality(100);
	// m_camera.setParameters(parameters);
	// m_camera.startPreview();
	//
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

	/**
	 * 拍照结束释放数据
	 */
	public void stopPreview() {
		if (m_camera != null) {
			// 当surfaceview关闭时，关闭预览并释放资源
			m_camera.setPreviewCallback(null);
			m_camera.stopPreview();
			m_camera.release();
			m_camera = null;
			isOver = false;
			count = 3;
		}
	}

	/**
	 * 打开摄像头设备
	 * 
	 * @throws IOException
	 */
	public void open() throws IOException {

		if (m_camera == null)
			m_camera = Camera.open();
		if (m_camera == null) {
			throw new IOException();
		}
		// 获取摄像头信息
		// Camera.Parameters parameters = m_camera.getParameters();
		// Log.i("*******", parameters.flatten());
	}

	/**
	 * 自动对焦
	 */
	@Override
	public void onAutoFocus(boolean success, Camera camera) {
		// TODO Auto-generated method stub

	}

	/**
	 * 通过PreViewCallback获取拍照数据
	 * 
	 * @author hb
	 * 
	 */
	public class PreViewCallback implements Camera.PreviewCallback {

		@Override
		public void onPreviewFrame(final byte[] data, Camera camera) {
			if (!isOver)// 默认直接返回,点击拍照才执行下面数据
				return;

			Size size = m_camera.getParameters().getPreviewSize();
			YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21,
					size.width, size.height, null);
			ByteArrayOutputStream outputSteam = new ByteArrayOutputStream();
			if (!yuvimage.compressToJpeg(
					new Rect(0, 0, size.width, size.height), 100, outputSteam)) {
				return;
			}
			if (--count == 0) {
				onPreviewListener.onPreviewListener(outputSteam.toByteArray());
				isOver = false;
			}
		}
	}



	/**
	 * 设置false 不让PreViewCallback获取图片数据
	 */
	public void startIsOver() {
		isOver = false;
	}

	/**
	 * 设置true 让PreViewCallback获取图片数据
	 */
	public void endIsOver() {
		isOver = true;
	}

	/**
	 * 设置图片接口对象
	 * 
	 * @param onPreviewListener
	 */
	public void setOnPreviewListener(OnPreviewListener onPreviewListener) {
		this.onPreviewListener = onPreviewListener;
	}

	/**
	 * 图片响应接口
	 * 
	 * @author hb
	 * 
	 */
	public interface OnPreviewListener {
		void onPreviewListener(byte[] data);
	}


}
